define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'fast'], function ($, undefined, Backend, Table, Form, Fast) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activityform/fromurl/index',
                    add_url: 'activityform/fromurl/add',
                    edit_url: 'activityform/fromurl/edit',
                    del_url: 'activityform/fromurl/del',
                    multi_url: 'activityform/fromurl/multi',
                    table: 'activityform/fromurl',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'title', title: __('Title'), align: 'left',
                            // formatter: function (value, row, index) {
                            //     return '<div class=""><a href="' + row.url + '" target="_blank">' + value + '</a></div>';
                            // }
                        },
                        // {field: 'qudao', title: __('Qudao')},
                        { field: 'type', title: __('Type'), },



                        // {field: 'name', title: __('Name')},
                        { field: 'sn', title: __('Sn') },
                        { field: 'fromurlques_ids', title: __('Fromurlques_ids'), operate: 'LIKE', formatter: Controller.api.formatter.fromurlques },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: "120px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('查看用户提交数据详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'activityform/fromurldata/detail?datahref=' + Fast.api.fixurl('') + 'activityform/fromurldata/customData?id=',
                                },
                                {
                                    name: 'export',
                                    title: __('用户提交数据详情导出'),
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-download',
                                    click: function (data, row) {
                                        window.open("fromurldata/export?id=" + row.ids, '_self');
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                fromurlques: function (value, row, index) {
                    if (typeof value === 'string' && value.includes(',')) {
                        // 将字符串用逗号分隔，转为数组
                        const valueArray = value.split(',').map(item => item.trim()); // 去除多余空格
                        // 遍历数组，生成每个 `<a>` 标签的 HTML
                        return valueArray
                            .map(item => {
                                const fullText = item; // 完整内容
                                const renderedText = fullText.length > 10 ? fullText.slice(0, 10) + '...' : fullText; // 截断内容
                                return `<a class="label" style="color: #000;font-weight: 500; font-size: 12px;" title="${fullText}">${renderedText}</a>`;
                            })
                            .join('<br>'); // 用换行符分隔每行
                    }
                    // 如果 value 为空或不是字符串，返回默认值
                    return '-';
                }

            },
        }
    };
    return Controller;
});